//
//  BNEditController.m
//  BeautifulNote
//
//  Created by Apple on 2019/7/17.
//  Copyright © 2019 Maker. All rights reserved.
//

#import "BNMoodEditController.h"

#import "BNChoseMoodView.h"
#import "NBChoseWeatherView.h"

#import <UITextView+Placeholder.h>
#import <YYImage.h>

@interface BNMoodEditController ()

@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UIButton *saveButton;
@property (nonatomic, strong) UIButton *moodButton;
@property (nonatomic, strong) UIButton *weatherButton;
@property (nonatomic, strong) UITextView *contentTextView;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) YYAnimatedImageView *weatherIV;
@property (nonatomic, assign) BNMoodType mood;
@property (nonatomic, assign) BNWeatherType weather;

@end

@implementation BNMoodEditController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initConstaints];

}


#pragma mark - [系统控件的<#Class#>Protocol]

#pragma mark - [自定义控件的<#Class#> Protocol]

#pragma mark - event response
- (void)clickBackButton {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)clickSaveButton {
    if (self.contentTextView.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入日记内容"];
        [self.contentTextView becomeFirstResponder];
        return;
    }

    BNHomeModel *model = [BNHomeModel new];
    model.content = self.contentTextView.text;
    model.weather = self.weather;
    model.mood = self.mood;
    
    NSCalendar *gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSInteger year = [gregorian component:NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger month = [gregorian component:NSCalendarUnitMonth fromDate:[NSDate date]];
    NSInteger day = [gregorian component:NSCalendarUnitDay fromDate:[NSDate date]];
    model.year = [NSString stringWithFormat:@"%ld", year];
    model.month = [NSString stringWithFormat:@"%ld", month];
    model.day = [NSString stringWithFormat:@"%ld", day];

    [BNDBHelper saveToDBWithHomeModel:model];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)clickMoodButton {
    [BNChoseMoodView showMoodViewWithSelectedBlock:^(BNMoodType mood) {
        self.contentTextView.textColor = [self getColorWitgMood:mood];
        self.mood = mood;
    }];
}

- (void)clickWeatherButton {
    [NBChoseWeatherView showWeatherViewWithSelectedBlock:^(BNWeatherType weather) {
        self.weatherIV.image = [self getImageWithWeathe:weather];
        self.weather = weather;
    }];
}

#pragma mark - public methods

#pragma mark - private methods
- (UIColor *)getColorWitgMood:(BNMoodType)mood {
    switch (mood) {
        case BNMoodTypeRed:
            return redMood;
            break;
        case BNMoodTypeOrange:
            return orangeMood;
            break;
        case BNMoodTypeYellow:
            return yellowMood;
            break;
        case BNMoodTypeGreen:
            return greenMood;
            break;
        case BNMoodTypeQingse:
            return qingseMood;
            break;
        case BNMoodTypeBlue:
            return blueMood;
            break;
        case BNMoodTypePurple:
            return purpleMood;
            break;
        case BNMoodTypeWhite:
            return whiteMood;
            break;
        case BNMoodTypeGray:
            return greenMood;
            break;
        case BNMoodTypeBlack:
            return blackMood;
            break;
        default:
            return whiteMood;
            break;
    }
}

- (YYImage *)getImageWithWeathe:(BNWeatherType)weather {
    switch (weather) {
        case BNWeatherTypeSunny:
            return [YYImage imageNamed:@"sunny.gif"];
            break;
        case BNWeatherTypeRain:
            return [YYImage imageNamed:@"rain.gif"];
            break;
        case BNWeatherTypeWindy:
            return [YYImage imageNamed:@"windy.gif"];
            break;
        case BNWeatherTypeSnow:
            return  [YYImage imageNamed:@"snow.gif"];
            break;
        default:
            return nil;
            break;
    }
}
- (void)initConstaints {
    
    [self.view addSubview:self.weatherIV];
    [self.weatherIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.backButton];
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(SafeAreaTopHeight + StatusBarHeight + 15);
        make.width.height.mas_equalTo(25);
    }];
    
    [self.view addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backButton);
        make.left.mas_equalTo(55);
        make.right.mas_equalTo(-55);
    }];
    
    [self.view addSubview:self.saveButton];
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.backButton);
        make.width.height.mas_equalTo(25);
    }];
    
    [self.view addSubview:self.moodButton];
    [self.moodButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ScreenWidth/ 2.0 - 20 - 30);
        make.top.mas_equalTo(self.backButton.mas_bottom).mas_equalTo(20);
        make.width.height.mas_equalTo(30);
    }];
    
    [self.view addSubview:self.weatherButton];
    [self.weatherButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ScreenWidth/ 2.0 + 20);
        make.top.mas_equalTo(self.backButton.mas_bottom).mas_equalTo(20);
        make.width.height.mas_equalTo(30);
    }];
    
    [self.view addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.weatherButton.mas_bottom).mas_equalTo(10);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(1);
    }];
    
    [self.view addSubview:self.contentTextView];
    [self.contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight - 50);
        make.top.mas_equalTo(self.line.mas_bottom).mas_equalTo(20);
    }];
    
}

#pragma mark - getters and setters
- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:16];
        _titleLb.textColor = [UIColor blackColor];
        _titleLb.textAlignment = NSTextAlignmentCenter;
        NSCalendar *gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
        NSInteger year = [gregorian component:NSCalendarUnitYear fromDate:[NSDate date]];
        NSInteger month = [gregorian component:NSCalendarUnitMonth fromDate:[NSDate date]];
        NSInteger day = [gregorian component:NSCalendarUnitDay fromDate:[NSDate date]];
        _titleLb.text = [NSString stringWithFormat:@"%ld.%ld.%ld", year, month, day];
    }
    return _titleLb;
}

- (UIButton *)saveButton {
    if (!_saveButton) {
        _saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveButton setImage:[UIImage imageNamed:@"save"] forState:UIControlStateNormal];
        [_saveButton addTarget:self action:@selector(clickSaveButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveButton;
}

- (UIButton *)moodButton {
    if (!_moodButton) {
        _moodButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moodButton setImage:[UIImage imageNamed:@"mood"] forState:UIControlStateNormal];
        [_moodButton addTarget:self action:@selector(clickMoodButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moodButton;
}

- (UIButton *)weatherButton {
    if (!_weatherButton) {
        _weatherButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_weatherButton setImage:[UIImage imageNamed:@"weather"] forState:UIControlStateNormal];
        [_weatherButton addTarget:self action:@selector(clickWeatherButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _weatherButton;
}

- (UIView *)line {
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = bnColor(237, 237, 237);
    }
    return _line;
}

- (UITextView *)contentTextView {
    if (!_contentTextView) {
        _contentTextView = [UITextView new];
        _contentTextView.placeholder = @"抒写心情日记..";
        _contentTextView.font = lightFont(16);
        _contentTextView.backgroundColor = [UIColor clearColor];
    }
    return _contentTextView;
}

- (YYAnimatedImageView *)weatherIV {
    if (!_weatherIV) {
        _weatherIV = [[YYAnimatedImageView alloc] init];
        _weatherIV.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _weatherIV;
}
@end
